package com.sxc.servletContext;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/sc01")
public class servletContext01 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取servletContext对象
        // (1)、通过request对象获取
        ServletContext servletContext1 = req.getServletContext();
        // (2)、通过Session对象获取
        ServletContext servletContext2 = req.getSession().getServletContext();
        // (3)、通过ServletConfig对象获取
        ServletConfig servletConfig = getServletConfig();
        ServletContext servletContext3 = servletConfig.getServletContext();
        // (4)、直接获取
        ServletContext servletContext4 = getServletContext();

        //常用方法
        //1. 获取当前服务器的版本信息
        String serverInfo = req.getServletContext().getServerInfo();
        System.out.println("获取当前服务器的版本信息: " + serverInfo);
        //2. 获取当前项目真实路径
        String realPath = req.getServletContext().getRealPath("/");
        System.out.println(" 获取当前服务器的真实路径: " + realPath);
    }
}
